(function (w) {

    var timerId = null;
    // 控制速度的快慢
    var time = 200;
    // 控制游戏的暂停和继续
    var isStop = false;
    // 分数显示
    var scores = 0;

    var that = null;
    // 游戏类，用来实例化游戏对象
    function Game(map, scoreTxT) {
        this.food = new Food();
        this.snake = new Snake();
        this.map = map;
        this.scoreTxT = scoreTxT;

        that = this;
    }

    // 游戏开始的方法
    Game.prototype.start = function () {
        this.food.render(this.map);
        this.snake.render(this.map);
        run();
        bindKey();

    }

    // 蛇动起来的方法
    function run() {
        timerId = setInterval(function () {
            this.snake.move();

            // 做边界判断
            var headX = this.snake.body[0].x;
            var headY = this.snake.body[0].y;
            var limitX = Math.floor(this.map.offsetWidth / this.snake.width);
            var limitY = Math.floor(this.map.offsetHeight / this.snake.height);

            if (headX >= limitX || headX < 0) {
                clearInterval(timerId);
                alert('Game over');
                return;
            }
            if (headY >= limitY || headY < 0) {
                clearInterval(timerId);
                alert('Game over');
                return;
            }

            this.snake.render(this.map);


            // 判断是否吃到食物
            var foodX = this.food.x;
            var foodY = this.food.y;
            var colors = ['pink', 'hotpink', 'skyblue', 'purple', 'yellowgreen', 'greenyellow', 'yellow'];
            if (headX == foodX && headY == foodY) {
                // 说明吃到食物了，要长身体
                var last = this.snake.body[this.snake.body.length - 1]
                this.snake.body.push({
                    x: last.x,
                    y: last.y,
                    color: colors[Math.floor(Math.random() * (colors.length - 1))],
                })

                // 加分数
                scores++;
                scoreTxT.innerText = scores;


                // 加快移动的速度，增加游戏难度
                time = time < 50 ? 50 : time - 10;
                clearInterval(timerId);
                run();

                this.food.render(map);
            }

            // 判断是否触碰到蛇自己的身体
            for (var i = 1; i < this.snake.body.length; i++) {
                if (this.snake.body[i].x == headX && this.snake.body[i].y == headY) {
                    clearInterval(timerId);
                    alert('Game over');
                    return;
                }
            }

        }.bind(that), time);
    }

    // 绑定方向键，并且按空格暂停和开始游戏
    // 蛇头不能直接回头
    function bindKey() {
        document.addEventListener('keydown', function (e) {
            // console.log(e.keyCode);
            switch (e.keyCode) {
                case 37:
                    if (this.snake.direction != 'right') {
                        this.snake.direction = 'left';
                    }
                    break
                case 38:
                    if (this.snake.direction != 'bottom') {
                        this.snake.direction = 'top';
                    }
                    break
                case 39:
                    if (this.snake.direction != 'left') {
                        this.snake.direction = 'right';
                    }
                    break
                case 40:
                    if (this.snake.direction != 'top') {
                        this.snake.direction = 'bottom';
                    }
                    break
                case 32: // 空格键暂停或者继续游戏
                    if (!isStop) {
                        isStop = true;
                        clearInterval(timerId);
                    } else {
                        isStop = false;
                        run();
                    }
                    break
            }
        }.bind(that));
    }

    w.Game = Game;
})(window)